package cn.tianjindong.blog_manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogArticleCommentMapper;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleComment;
import cn.tianjindong.blog.pojo.BlogArticleCommentExample;
import cn.tianjindong.blog.pojo.BlogArticleExample;
import cn.tianjindong.blog_manager.service.CommentService;

@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	private BlogArticleCommentMapper commentMapper;
	
	@Override
	public int getCommentCount() {
		int count = commentMapper.countByExample(new BlogArticleCommentExample());
		return count;
	}

	@Override
	public PageView<BlogArticleComment> getCommentPageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogArticleComment> list = commentMapper.getCommentList(startIndex, countItem);

		// 获取数据库总条数
		int totalItem = commentMapper.countByExample(new BlogArticleCommentExample());

		PageView<BlogArticleComment> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		return pageView;
	}

	@Override
	public BlogArticleComment getCommentById(Long commentId) {
		BlogArticleComment comment = commentMapper.selectByPrimaryKey(commentId);
		return comment;
	}

	@Override
	public void deleteCommentById(Long commentId) {
		commentMapper.deleteByPrimaryKey(commentId);
	}

}
